require 'fpdf'
require 'chinese'

class OrdersController < ApplicationController
  before_filter :login_required
  before_filter :check_merchandiser_right,:except=>[:show,:new,:create]
  before_filter :find_user,:only=>[:index,:new,:create,:edit]
  # GET /orders
  # GET /orders.xml
  def index
    if merchandiser?
      @orders = Order.paginate :page => params[:page],:per_page => 10,:order=>'id desc'
    else
      @orders = @user.orders.paginate :page => params[:page],:per_page => 10,:order=>'id desc'
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @orders }
    end
  end

  # GET /orders/1
  # GET /orders/1.xml
  def show
    @order = Order.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @order.products.to_xml(:skip_types => true,:dasherize => false,
          :only=>[:id,:style_id,:fm,:bm,:chest,:frd,:fld,:brd,:name,:price])}
    end
  end

  # GET /orders/new
  # GET /orders/new.xml
  def new
    if !@cart.items.empty?
      @order = Order.new
      @order_owen = @user
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @order }
      end
    else
      redirect_to :action=>:my_cart,:controller=>:store
    end
  end

  # GET /orders/1/edit
  def edit
    @order = Order.find(params[:id])
    @order_owen = @order.user
  end

  # POST /orders
  # POST /orders.xml
  def create
    @order = @user.orders.build(params[:order])
    @order.add_line_items_from_cart(@cart)

    if params[:address]
      address = @user.addresses.build(params[:address])
      @order.address = address if address.save
    end

    if @order.used_point.to_i > 0
      @order.save_and_decrease_user_point
    else
      @order.save
    end

    
    if @order.errors.empty?
      session[:cart] = nil
      #      OrderMailer.deliver_confirm(@user.email)
      respond_to do |format|
        format.html { redirect_to_tips("谢谢惠顾，您的订单已确认，我们会尽快发货") }
        format.xml  { render :xml => @order, :status => :created, :location => @order}
      end
    else
      respond_to do |format|
        format.html { render :action => "new" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
    
  end

  # PUT /orders/1
  # PUT /orders/1.xml
  def update
    @order = Order.find(params[:id])
    if @order.status != 3 and params[:order][:status] == "3"
      @order.calculate_point(params[:order])
    else
      @order.update_attributes(params[:order])
    end
    
    respond_to do |format|
      if @order.errors.empty?
        flash[:notice] = '订单更新成功.'
        format.html { redirect_to('/store/mine') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.xml
  def destroy
    @order = Order.find(params[:id])
    @order.destroy

    respond_to do |format|
      format.html { redirect_to('/store/mine') }
      format.xml  { head :ok }
    end
  end

  def print
    pdf=FPDF.new('P','mm',[215,140])
    pdf.extend(PDF_Chinese)
    pdf.AddPage
    ic = Iconv.new('GB2312', 'UTF-8')
    pdf.AddGBFont("song",ic.iconv("仿宋_GB2312"))
    pdf.SetFont("song",'B',15)
    pdf.Cell(195,8,ic.iconv("肇庆奔驰五金维修工具经营部 出货单"),0,1,"C")

    send_data pdf.Output,:filename => "order#{params[:id]}.pdf",
      :type => "application/pdf",
      :disposition => 'inline'
  end


end
